package com.penghaisoft.microwms.base.web.auth.controller;

import com.penghaisoft.microwms.base.web.auth.service.IRoleService;
import com.penghaisoft.microwms.common.base.model.BaseResRole;
import com.penghaisoft.microwms.common.base.model.BaseResRoleFactory;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/resrole")
@Slf4j
public class BaseResRoleController extends BaseController {

	@Autowired
	private IRoleService roleService;

	/**
	 * 新增记录
	 * 
	 * @param baseResrole
	 * @return
	 */
	@PostMapping("c")
	public TransResult create(BaseResRole baseResrole) {
		preInsert(baseResrole);
		Resp resp = roleService.add(baseResrole);
		return fromResp(resp);
	}

	/**
	 * 删除记录（逻辑删除active_flag=0)
	 * 
	 * @param baseResroles
	 * @return
	 */
	@PostMapping("d")
	public TransResult delete(@RequestParam(name = "resroleIds") List<String> resroleIds) {
		Resp resp = roleService.deleteRoles(resroleIds);
		return fromResp(resp);
	}

	/**
	 * 查询列表
	 * 
	 * @param
	 * @return
	 */
	@PostMapping("list")
	public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows, BaseResRole baseResRole) {
		Pager<BaseResRole> result = roleService.queryList(page, rows, baseResRole);
		return success(result);
	}

	/**
	 * 查询单条
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("r/{id}")
	public TransResult queryById(@PathVariable String id) {
		BaseResRole baseResrole = roleService.queryById(id);
		return success(baseResrole);
	}

	/**
	 * 修改记录
	 * 
	 * @param baseResrole
	 * @return
	 */
	@PostMapping("u")
	public TransResult update(BaseResRole baseResrole) {
		preUpdate(baseResrole);
		Resp resp = roleService.update(baseResrole);
		return fromResp(resp);
	}
	/**
	 * @Description:查询当前角色下的工厂
	 * @author jzh
	 * @date 2017/3/23 17:59
	 * @param
	 * @return 
	 */
	@GetMapping("/{resroleId}/factory")
	public TransResult queryAllFactory(@PathVariable String resroleId){
		BaseResRoleFactory condition = new BaseResRoleFactory();
		if (!resroleId.equals("all")){
			condition.setResroleId(resroleId);
		}
		List<BaseResRoleFactory> factorys = roleService.queryAllFactory(condition);
		return success(factorys);
	}
	/**
	 * @Description: 给角色分配工厂
	 * @author jzh
	 */
	@PostMapping("/u/factory")
	public TransResult assignRoleResFactorys(@RequestBody List<BaseResRoleFactory> list){
		Resp resp = roleService.RoleResFactory(list);
		return fromResp(resp);
	}

	/**
	 * 查询满足条件的角色列表(非分页用)
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("list/all")
	public TransResult queryById() {
		BaseResRole baseResrole = new BaseResRole();
		List<BaseResRole> rtList = roleService.queryAllRoleList(baseResrole);
		return success(rtList);
	}

	public BaseResRole preInsert(BaseResRole baseResrole) {
		if (StringUtils.isEmpty(baseResrole.getCreateBy())) {
			baseResrole.setCreateBy(getLoginName());
		}
		/*
		 * if (ObjectUtils.isEmpty(baseResrole.getGmtCreate())) {
		 * baseResrole.setGmtCreate(new Date()); }
		 */
		return baseResrole;
	}

	public BaseResRole preUpdate(BaseResRole baseResrole) {
		if (StringUtils.isEmpty(baseResrole.getLastModifiedBy())) {
			baseResrole.setLastModifiedBy(getLoginName());
		}
		if (ObjectUtils.isEmpty(baseResrole.getGmtModified())) {
			baseResrole.setGmtModified(new Date());
		}
		return baseResrole;
	}
	
}
